/**
 * Created by nicholasyan on 16/8/31.
 */
var DataTable = function () {
    function __initialize(json, options) {
        this.originData = $.extend(true, [], json);
        this.datasource = json;
        this.options = options;
        this.initData();
    }

    function isjson(obj) {
        return typeof(obj) == "object" &&
            Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
    }

    __initialize.prototype = {
        initData: function () {
            for (var i = 0; i < this.originData.length; i++) {
                this.datasource[i]["$status"] = "rsNone";
            }
        },
        addRow: function (row) {
            row["$status"] = "rsNew";
            //for (var i = 0; i < this.originData.length; i++) {
            //    if (this.originData[i][this.options.id] == row[this.options.id]) {
            //        row["$status"] = "rsUpdate";
            //        break;
            //    }
            //}
            this.datasource.push(row);
        },
        deleteRow: function (primaryKey) {
            for (var i = 0; i < this.datasource.length; i++) {
                if (this.datasource[i][this.options.primaryKey] == primaryKey) {
                    this.datasource.splice(i, 1);
                    break;
                }
            }
            for (var i = 0; i < this.originData.length; i++) {
                if (this.originData[i][this.options.primaryKey] == primaryKey) {
                    this.originData[i]["$status"] = "rsDelete";
                    break;
                }
            }
        },
        setRowValue: function (primaryKey, row) {
            for (var i = 0; i < this.datasource.length; i++) {
                if (this.datasource[i][this.options.primaryKey] == primaryKey) {
                    this.datasource[i] = row;
                    this.datasource[i]["$status"] = "rsUpdate";
                    break;
                }
            }
        },
        getChanges: function () {
            var datas = [];
            for (var i = 0; i < this.datasource.length; i++) {
                if (this.datasource[i]["$status"] != "rsNone") {
                    datas.push(this.datasource[i]);
                }
            }
            for (var i = 0; i < this.originData.length; i++) {
                if (this.originData[i]["$status"] == "rsDelete") {
                    datas.push(this.originData[i]);
                }
            }
            return datas;
        }
    };
    return __initialize;

}();